package net.sudot.chess.business.wechat.handler;

import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import net.sudot.chess.business.model.SocialUser;
import net.sudot.chess.business.service.SocialUserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 微信取消关注事件处理
 *
 * @author tangjialin on 2017-05-04 0004.
 */
@Component
public class UnsubscribeHandler extends AbstractHandler {

    @Resource
    private SocialUserService socialUserService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService wxMpService,
                                    WxSessionManager sessionManager) {
        String fromUser = wxMessage.getFromUser();
        if (fromUser == null) { return null; }
        SocialUser socialUser = socialUserService.findByUniqueId(wxMpService.getWxMpConfigStorage().getAppId(), fromUser);
        if (socialUser == null) { return null; }
        socialUser.setSubscribe(false);
        socialUserService.update(socialUser);
        return null;
    }

}
